package com.survey.Controller;


import com.survey.pojo.data.AdminDO;
import com.survey.service.AdminService;
import com.survey.pojo.vo.AdminVO;
import com.survey.utils.Result;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {

    @Resource
    AdminService adminService;

    @RequestMapping("/admin/login")
    @ResponseBody
    public Result login(String username,String pwd,HttpServletRequest request) throws Exception {
            Md5Hash md5Hash = new Md5Hash(pwd, username);
            AdminVO adminVO = new AdminVO();
            adminVO.setUser(username);
            adminVO.setPassword(md5Hash.toString());
            List<AdminDO> adminDOS =
                        adminService.listAdmin(adminVO);
            if(adminDOS.isEmpty()){
                return new Result(Result.CODE_FAILURE);
            }
           AdminDO admin = adminDOS.get(0);
            request.getSession().setAttribute("admin",admin);
            return new Result(Result.CODE_SUCCESS);
    }

    /**
     * userName: wxiao_cherish
     * email: wxiao_cherish@qq.com
     * phone: 18796267140
     * userPw: 111111
     * confirmPwd: 111111
     * submit: 完成
     */
    @RequestMapping("/admin/adminAdd")
    @ResponseBody
    public Result addAdmin(String username,String pwd,AdminVO adminVO,HttpServletRequest request) throws Exception {
        AdminVO newAdmin = new AdminVO();
        newAdmin.setUser(username);
        List<AdminDO> adminDOS = adminService.listAdmin(newAdmin);
        if(adminDOS.isEmpty()){
            adminVO.setUser(username);
            adminVO.setPassword(new Md5Hash(pwd,username).toString());
            System.out.println(adminVO);
            return adminService.addAdmin(adminVO);
        }
       return new Result(Result.CODE_FAILURE,"用户名重复");
    }

    /**
     * aid=
     * "&username="
     *  + "&email="
     *  + "&phone="
     *  + "&oldpwd="
     *  + "&pwd="
     *  + "&confirmPwd
     */
    @RequestMapping("/admin/modify")
    @ResponseBody
    public Result modify(AdminVO adminVO, Integer aid,String oldpwd,String pwd, HttpServletRequest request,HttpSession session) throws Exception {
        adminVO.setId(aid);
        AdminDO detailAdmin = adminService.findDetailAdmin(adminVO);
        Md5Hash oldPassword = new Md5Hash(oldpwd, adminVO.getUser());
        if(detailAdmin.getPassword().equals(oldPassword.toString())){
            Md5Hash password = new Md5Hash(pwd, adminVO.getUser());
            adminVO.setPassword(password.toString());
            AdminDO admin=new AdminDO();
            BeanUtils.copyProperties(admin,adminVO);
            Result result = adminService.updateAdmin(adminVO);
            if(result.getCode()==200){
                session.setAttribute("admin",admin);
                return new Result(Result.CODE_SUCCESS);
            }
        }
        return new Result(Result.CODE_FAILURE,"修改失败");
    }


    /**
     * 查询
     */
    @RequestMapping("/admin/search")
    public String search(Integer aid,Model model) throws Exception {
        AdminVO adminVO=new AdminVO();
        adminVO.setId(aid);
        AdminDO detailAdmin = adminService.findDetailAdmin(adminVO);
        model.addAttribute("admin",detailAdmin);
        return "/admin/AdminModify.jsp";
    }


    @RequestMapping("/logout")
    public void logout(HttpServletResponse response, HttpSession session) throws IOException {
        session.invalidate();
        response.sendRedirect("index1.jsp");
    }
}
